package com.xavier.convertor;

import cn.hutool.core.util.ObjectUtil;
import com.xavier.utils.BeanUtil;
import com.xavier.model.User;
import com.xavier.dto.UserDTO;
import com.xavier.vo.UserPageVO;
import com.xavier.vo.UserDetailVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 用户对象相互转换
 *
 * @author Xavier
 * @version 1.0
 */
public abstract class UserConvertor {

    public static UserPageVO toUserPageVO(User user) {
        if (Objects.isNull(user)) {
            return null;
        }
        UserPageVO vo = BeanUtil.beanCopy(user, UserPageVO.class);
        return vo;
    }

    public static UserDetailVO toUserDetailVO(User user) {
        if (Objects.isNull(user)) {
            return null;
        }
        UserDetailVO vo = BeanUtil.beanCopy(user, UserDetailVO.class);
        return vo;
    }

    public static User toUser(UserDTO dto) {
        if (Objects.isNull(dto)) {
            return null;
        }
        User user = BeanUtil.beanCopy(dto, User.class);
        return user;
    }

    public static List<UserPageVO> toUserPageVOList(List<User> userList) {
        if (ObjectUtil.isEmpty(userList)) {
            return new ArrayList<>();
        }
        List<UserPageVO> userInfoList = new ArrayList<>(userList.size());
        for (User user : userList) {
            userInfoList.add(toUserPageVO(user));
        }
        return userInfoList;
    }

    public static List<User> toUserList(List<UserDTO> dtoList) {
        if (ObjectUtil.isEmpty(dtoList)) {
            return new ArrayList<>();
        }
        List<User> userList = new ArrayList<>(dtoList.size());
        for (UserDTO dto : dtoList) {
            userList.add(toUser(dto));
        }
        return userList;
    }

}
